<?php
	/* ***************************************************** */
	/* Arcans Team (2010), website                           */
	/* Site under GNU GPL v3 Licence                         */
	/* (see NOTICE and COPYING in the of the site file tree) */
	/* ***************************************************** */

	/* ***************************************************** */
	/* Fonction de modification de la page d'accueil         */
	/* ***************************************************** */
	function accueilFun ($conf,$attr_tab) {
		$connect=0; if (isset($_SESSION["pseudo"])) $connect=1;
		return array("mod"=>true,"fun"=>"index","connect"=>$connect);
		//
		//TODO
		//
	}
	/* ***************************************************** */
	/* Fonction de remplissage de la liste des projets       */
	/* ***************************************************** */
	function listProjetsFun ($conf,$attr_tab) {
		$bdd = bdd_connect("mysql",$conf); $result = $bdd->query("SELECT * FROM at_projets WHERE niveau>0 AND niveau!=8 ORDER BY nom;");
		$modo=0; if (isset($_SESSION["level"]) && $_SESSION["level"] >= 50) $modo=1;
		$admin=0; if ($modo && $_SESSION["level"] == 100) $admin=1; $cnt=0; $prjts=array();
		if ($result->rowCount() > 0) {
			$c1=0; $c2=0; $c3=0; $c4=0; $iter=0; $pjt_at=array(); $pjt_mod=array(); $pjt_game=array(); $pjt_oth=array(); $pjts_enc=array();
			while ($sql_r = $result->fetch()) {
				$data=array(); $data["fun"]="pjts"; $data["tr_tag"]=array("id"=>"pjt_".$sql_r["id"]); $data["nom"]=$sql_r["nom"];
				$data["ann_mod"]=array("onclick"=>"voile.initPclose('enc1_mod_".$sql_r["id"]."');");
				$data["nom_ad"]=array("href"=>$conf[2]."projet/".$sql_r["id"],"texte"=>$sql_r["nom"]);
				switch ($sql_r["niveau"]) {
					case 1: $data["niv"]="accepté";break; case 2: $data["niv"]="analyse";break; case 3: $data["niv"]="analysé";break;
					case 4: $data["niv"]="écriture";break; case 5: $data["niv"]="écrit";break; case 6: $data["niv"]="vérification";break;
					case 7: $data["niv"]="vérifié";break;
				}
				$data["pg"]="no";
				if ($sql_r["niveau"]==2 || $sql_r["niveau"]==4 || $sql_r["niveau"]==6)
					$data["pg"]=array("value"=>$sql_r["rendu"],"max"=>"100");
				if ($sql_r["date_maj"] != "") $data["date"] = date("d/m/y H:i",strtotime($sql_r["date_maj"]));
				else $data[$iter]["date"] = date("d/m/y H:i",strtotime($sql_r["date_post"]));
				$data["modo"] = $modo;
				if ($modo) $data["mod_r"]=array("href"=>"#","onclick"=>"projets.mod_pjt('".$sql_r["id"]."');");
				switch ($sql_r["categorie"]) {
					case "1": $pjt_at[$c1]=$data; $c1++; break; case "2": $pjt_mod[$c2]=$data; $c2++; break;
					case "3": $pjt_game[$c3]=$data; $c3++; break; default: $pjt_oth[$c4]=$data; $c4++;
				}
				//
				$pjts_enc[$iter]["enc1_mod"]=array("id"=>"enc1_mod_".$sql_r["id"]);
				//
				$iter++;
				//
				//TODO
				//
			}
		}
		return array("mod"=>true,"fun"=>"pjts","modo"=>$modo,"admin"=>$admin,"c1"=>$c1,"c2"=>$c2,"c3"=>$c3,"c4"=>$c4,"iter"=>$iter,
			"pjt_at"=>$pjt_at,"pjt_mod"=>$pjt_mod,"pjt_game"=>$pjt_game,"pjt_oth"=>$pjt_oth,"pjts_enc"=>$pjts_enc);
	}
	/* ***************************************************** */
	/* Fonction de création de la page de projet             */
	/* ***************************************************** */
	function projetFun ($conf,$attr_tab) {
		$pjt_id = filter_input(INPUT_GET,"id",FILTER_VALIDATE_INT); if ($pjt_id === FALSE) $pjt_id = 0;
		$return=array("mod"=>true,"fun"=>"pjt");
		$bdd = bdd_connect("mysql",$conf); $result = $bdd->query("SELECT * FROM at_projets WHERE id=".$pjt_id);
		if ($result->rowCount() == 0) $return["access"]=0;
		else {
			$result = $result->fetch(); if ($result["niveau"]==0) $return["access"]=2;
			else {
				$return["access"]=1; $return["nom"]=$result["nom"];
				$return["datec"]=date("d/m/y H:i",strtotime($result["date_post"]));
				$return["datem"]=date("d/m/y H:i",strtotime($result["date_maj"]));
				switch ($result["niveau"]) {
					case 1: $return["niv"] = "accepté"; break; case 2: $return["niv"] = "analyse"; break;
					case 3: $return["niv"] = "analysé"; break; case 4: $return["niv"] = "écriture"; break;
					case 5: $return["niv"] = "écrit"; break; case 6: $return["niv"] = "vérification"; break;
					case 7: $return["niv"] = "vérifié"; break; case 8: $return["niv"] = "archivé"; break;
				}
				$return["pg"]="no";
				if ($result["niveau"]==2 || $result["niveau"]==4 || $result["niveau"]==6)
					$return["pg"]=array("max"=>"100","value"=>$result["rendu"]);
				//
				//
				$result2 = $bdd->query("SELECT COUNT(*) nb_part FROM at_pjt_user WHERE id_pjt=".$pjt_id." AND (statut=1 OR statut=2);");
				//
				$result2 = $result2->fetch(); $return["parts"] = "";
				//
// 				$result2 = $bdd->query("SELECT COUNT(*) nb_part FROM at_pjt_user WHERE id_pjt=".$pjt_id." AND (statut=1 OR statut=2);");
// 				$result2 = $result2->fetch(); $texte = "Il n'y a aucun participants au projet actuellement.";
// 				if ($result2["nb_part"] != 0) $texte = "Il y a ".$result2["nb_part"]." participants.";
// 				$result2 = $bdd->query("SELECT COUNT(*) nb_prop FROM at_pjt_user WHERE id_pjt=".$pjt_id." AND statut=3;");
// 				$result2 = $result2->fetch();
// 				if ($result2["nb_prop"] == 1) $texte .= " (1 proposition)";
// 				elseif ($result2["nb_prop"] > 1) $texte .= " (".$result2["nb_prop"]." propositions)";
// 				$link = array("href"=>"#","texte"=>$texte,"onclick"=>"projet.init(".$pjt_id.',"show_parts");');
// 				addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
				//
				//
			}
		}
		return $return;
		//
		//TODO
		//
		/*
				$row = addNode($xml_page,"tr",$table,array()); addNode($xml_page,"td",$row,array("texte"=>"Participants :","class"=>"bold"));
				$result2 = $bdd->query("SELECT COUNT(*) nb_part FROM at_pjt_user WHERE id_pjt=".$pjt_id." AND (statut=1 OR statut=2);");
				$result2 = $result2->fetch(); $texte = "Il n'y a aucun participants au projet actuellement.";
				if ($result2["nb_part"] != 0) $texte = "Il y a ".$result2["nb_part"]." participants.";
				$result2 = $bdd->query("SELECT COUNT(*) nb_prop FROM at_pjt_user WHERE id_pjt=".$pjt_id." AND statut=3;");
				$result2 = $result2->fetch();
				if ($result2["nb_prop"] == 1) $texte .= " (1 proposition)";
				elseif ($result2["nb_prop"] > 1) $texte .= " (".$result2["nb_prop"]." propositions)";
				$link = array("href"=>"#","texte"=>$texte,"onclick"=>"projet.init(".$pjt_id.',"show_parts");');
				addNode($xml_page,"a",addNode($xml_page,"td",$row,array()),$link);
				//
				addNode($xml_page,"td",$row,array("texte"=>"lien vers le projet :","class"=>"bold"));
				$data_xml = new DomDocument(); $data_xml->loadXML($result["lien"]); $data = $data_xml->documentElement;
				if ($data->hasChildNodes()) $cell->appendChild($xml_page->importNode($data->firstChild,true));
				else $cell->appendChild($xml_page->createTextNode("Pas de lien"));
				$row = addNode($xml_page,"tr",$table,array());
				addNode($xml_page,"td",$row,array("texte"=>"Description :","class"=>"bold"));
				$data_xml->loadXML($result["description"]); $data = $data_xml->documentElement;
				if ($data->hasChildNodes()) {
					$cell = addNode($xml_page,"td",addNode($xml_page,"tr",$table,array()),array("colspan"=>"2","style"=>"padding:10px;"));
					foreach ($data->childNodes as $child) $cell->appendChild($xml_page->importNode($child,true));
				}
				else addNode($xml_page,"td",$row,array("texte"=>"Pas de description disponible."));
				$row = addNode($xml_page,"tr",$table,array());
				addNode($xml_page,"h4",addNode($xml_page,"td",$row,array()),array("texte"=>"Derniers objectifs :"));
				addNode($xml_page,"h4",addNode($xml_page,"td",$row,array()),array("texte"=>"Dernier message :"));
				$row = addNode($xml_page,"tr",$table,array());
				$cell = addNode($xml_page,"td",$row,array());
				$query = "SELECT * FROM at_objectifs WHERE id_projet=".$pjt_id." AND niveau>0";
				$result = $bdd->query($query." ORDER BY date_maj,date_post DESC LIMIT 5;");
				if ($result->rowCount() == 0) {
					$result = "Il n'y a pas d'objectif pour ce projet.";
					addNode($xml_page,"p",$cell,array("id"=>"no_objpage","texte"=>$result));
				}
				else {
					$table_obj = addNode($xml_page,"table",$cell,array("class"=>"obs_tab","style"=>"font-size:0.9em;"));
					$row_obj = addNode($xml_page,"tr",$table_obj,array());
					addNode($xml_page,"th",$row_obj,array("texte"=>"Noms :"));
					addNode($xml_page,"th",$row_obj,array("texte"=>"P"));
					addNode($xml_page,"th",$row_obj,array("texte"=>"Niv. :"));
					addNode($xml_page,"th",$row_obj,array("texte"=>"date mod. :"));
					while ($elt = $result->fetch()) {
						$row_obj = addNode($xml_page,"tr",$table_obj,array("id"=>"objpage_".$elt["id"]));
						addNode($xml_page,"td",$row_obj,array("texte"=>$elt["nom"]));
						$cell = addNode($xml_page,"td",$row_obj,array());
						addNode($xml_page,"div",$cell,array("class"=>"prior_d prior".$elt["priorite"]."_d"));
						$niveau = "";
						switch ($elt["niveau"]) {
							case 1: $niveau="accepté"; break; case 2: $niveau="analyse"; break;
							case 3: $niveau="analysé"; break; case 4: $niveau="écriture"; break;
							case 5: $niveau="écrit"; break; case 6: $niveau="vérification"; break;
							case 7: $niveau="vérifié"; break;
						}
						addNode($xml_page,"td",$row_obj,array("texte"=>$niveau));
						$date = "";
						if ($elt["date_maj"] == NULL) $date = $elt["date_post"];
						else $date = $elt["date_maj"];
						addNode($xml_page,"td",$row_obj,array("texte"=>date("d/m/y H:i",strtotime($date))));
					}
				}
				$cell = addNode($xml_page,"td",$row,array());
				$query = "SELECT at_messages.date_post,at_messages.message,at_users.nom FROM at_messages,at_users WHERE";
				$query .= " at_messages.type_obt=0 AND at_messages.id_obt=".$pjt_id." AND at_messages.id_user=at_users.id";
				$result = $bdd->query($query." ORDER BY date_maj,date_post DESC LIMIT 1;");
				if ($result->rowCount() == 0) {
					$result = "Il n'y a pas de message pour ce projet.";
					addNode($xml_page,"p",$cell,array("id"=>"no_messpage","texte"=>$result));
				}
				else {
					$result = $result->fetch();
					$table_mess = addNode($xml_page,"table",$cell,array("id"=>"messpage","style"=>"margin:0px;"));
					$row_mess = addNode($xml_page,"tr",$table_mess,array());
					$cell_mess = addNode($xml_page,"td",$row_mess,array("texte"=>"auteur : ","style"=>"font-weight:bold;"));
					addNode($xml_page,"span",$cell_mess,array("texte"=>$result["nom"],"style"=>"font-weight:normal;"));
					$cell_mess = addNode($xml_page,"td",$row_mess,array("texte"=>"date : ","style"=>"text-align:right;font-weight:bold;"));
					$date = date("d/m/y H:i",strtotime($result["date_post"]));
					addNode($xml_page,"span",$cell_mess,array("texte"=>$date,"style"=>"font-weight:normal;"));
					$row_mess = addNode($xml_page,"tr",$table_mess,array());
					$cell_mess = addNode($xml_page,"td",$row_mess,array("colspan"=>"2","style"=>"border:solid 1px #aaa;"));
					$data_xml->loadXML($result["message"]); $data = $data_xml->documentElement;
					foreach ($data->childNodes as $child) $cell_mess->appendChild($xml_page->importNode($child,true));
				}
				$row = addNode($xml_page,"tr",$table,array()); $cell = addNode($xml_page,"td",$row,array());
				$js = "projet.init(".$pjt_id.',"show_objs");';
				addNode($xml_page,"a",$cell,array("href"=>"#","onclick"=>$js,"texte"=>"Accéder aux objectifs"));
				$cell = addNode($xml_page,"td",$row,array()); $js = "projet.init(".$pjt_id.',"show_mess");';
				addNode($xml_page,"a",$cell,array("href"=>"#","onclick"=>$js,"texte"=>"Accéder aux messages"));
		}*/
	}
	/* ***************************************************** */
	/* Fonction de création de la liste des utilisateurs     */
	/* ***************************************************** */
	function listusersFun ($conf,$attr_tab) {
		$bdd = bdd_connect("mysql",$conf); $result = $bdd->query("SELECT * FROM at_users;");
		$modo=0; if ($_SESSION["level"] >= 50) $modo=1; $users = array(); $iter=0;
		while ($sql_r = $result->fetch()) {
			$users[$iter]["fun"] = "listu"; $users[$iter]["tr_tag"] = array("id"=>"user_".$sql_r["id"]);
			$users[$iter]["nom_ad"] = array("href"=>$conf[2]."user/".$sql_r["id"],"texte"=>$sql_r["nom"]);
			switch($sql_r["niveau"]) {
				case "5": $users[$iter]["rang"]="testeur"; break; case "10": $users[$iter]["rang"]="développeur"; break;
				case "50": $users[$iter]["rang"]="modérateur"; break; case "100": $users[$iter]["rang"]="administrateur"; break;
				case "-10": $users[$iter]["rang"]="inactif"; break;
			}
			$users[$iter]["ins"] = date("d/m/y H:i",strtotime($sql_r["creation"])); $users[$iter]["modo"] = $modo;
			if ($modo) { $users[$iter]["email"] = $sql_r["email"];
				$users[$iter]["mod_r"] = array("onclick"=>"users.mod_user(".$sql_r["id"].");","href"=>"#"); }
			$iter++;
		}
		$admin=0; if ($_SESSION["level"] == 100) $admin=1;
		return array("mod"=>true,"fun"=>"listu","modo"=>$modo,"admin"=>$admin,"users"=>$users);
	}
	/* ***************************************************** */
	/* Fonction de création de la page de compte utilisateur */
	/* ***************************************************** */
	function compteFun ($conf,$xml_page,$xml_head,$xml_body,$attr_tab) {
		$xml_bdd = new DomDocument(); $xml_bdd->load($conf[5]); $bdd = $xml_bdd->getElementsByTagName("bdd");
		foreach ($bdd as $elt) if ($elt->getAttribute("name")=="pgsql") { $bdd = $elt; break; }
		$bdd = new PDO ($bdd->getAttribute("value0"),$bdd->getAttribute("value1"),$bdd->getattribute("value2"));
		//
		//add
		//
		//TODO
		//
	}
	/* ***************************************************** */
	/* Fonction de remplissage de la liste des objectifs     */
	/* ***************************************************** */
	function listArchsFun ($xml_page,$xml_head,$xml_body,$attr_tab) {
		$xml_archs = new DomDocument(); $xml_archs->load($attr_tab[0]);
		$nb_archs = $xml_archs->getElementsByTagName("arch")->length;
		if ($nb_archs > 0) {
			//TODO : on vera plus tard pour les archives
		}
	}
?>